﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

using org.pdfbox.pdmodel;
using org.pdfbox.pdmodel.common;
using org.pdfbox.util;
using org.pdfbox.pdfwriter;
using org.pdfbox.io;

namespace WindowsFormsApplication
{
    public partial class PdfBoxForm : Form
    {
        public PdfBoxForm()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            openFileDialog1.ShowDialog();            
            string file1Name = openFileDialog1.FileName;
            openFileDialog1.ShowDialog();
            string file2Name = openFileDialog1.FileName;
            PDDocument resultFile = MergePDF(file1Name, file2Name);
            saveFileDialog1.ShowDialog();
            string resultFileName = saveFileDialog1.FileName;
            resultFile.save(resultFileName);
        }

        private PDDocument MergePDF(string file1, string file2)
        {
            PDDocument resultFile = new PDDocument();
            PDDocument pdf1 = PDDocument.load(file1);
            PDDocument pdf2 = PDDocument.load(file2);

            java.util.List list = pdf1.getDocumentCatalog().getAllPages();
            for (int i = 0; i < 3; i++)
            {
                PDPage page = list.get(i) as PDPage;
                resultFile.addPage(page);
            }
            list = pdf2.getDocumentCatalog().getAllPages();
            for (int i = 0; i < 3; i++)
            {
                PDPage page = list.get(i) as PDPage;
                resultFile.addPage(page);
            }
            return resultFile;
        }

        private string GetPDFFile(string filename)
        {
            PDDocument doc = PDDocument.load(filename);
            PDFTextStripper stripper = new PDFTextStripper();
            return stripper.getText(doc);
        }

        private void AddTextToPDF(string text)
        {
            PDDocument doc = new PDDocument();           
        }
    }
}
